GBML manual / Audio

Sound and music

The GBA plays one song at a time plus up to four sound effects on top, and GBML gives you separate functions for each because they work differently.

Two kinds of audio

A game makes noise in two different ways, and GBML keeps them apart.

The two are different kinds of file. A song is a tracker module: a file that holds instruments (small recorded sounds) and a score that says which instrument plays which note and when. Song files end in .mod, .xm, .s3m or .it. A sound effect is a plain recording, a .wav file. In GBML they are two different types, song and sound, and you name one by writing its asset name as a plain word: music_play(song_theme, true, 1) or sound_play(snd_coin, 1). Giving a sound to a music function, or the other way round, is an error the checker reports before the game is built.

Every volume in GBML is a number from 0 (silent) to 1 (full).

Music: one song, and how to change it

Only one song plays at a time. Changing songs is where the two functions differ.

The fade is always out first, then in - never two songs blended together. The engine fades out over 16 frames and in over about 24 (25, counting the frame it arrives on). A frame is one picture the GBA draws, and it draws about 60 of them every second, so that is roughly a quarter of a second out and four-tenths of a second in.

music_fade_to does nothing if the song you ask for is already playing, or is already being faded to. That is deliberate: if two rooms use the same song, walking between them does not restart it. music_fade_to(nothing, true, 1) fades to silence.

music_stop stops the music at once. music_current tells you which song was last asked for, even part-way through a fade.

Fades keep running while the game is paused, so a pause menu with its own music still fades properly.

Asking about a fade

music_current says where the music is heading, and that changes the instant you call music_fade_to - the old song can go on playing for another quarter of a second. Two more questions tell you what is really happening:

There is no "how far through the fade" number. A fade can start half way down, or end quieter than full volume, so a single 0 to 1 figure would not mean the same thing every time; the volume does.

Bending a song while it plays

Three functions change how the current song plays. They do nothing if no song is playing.

FunctionWhat it changesRange
music_pitchHow high or low the notes sound. 2 is an octave up, 0.5 an octave down.0.5 to 2
music_tempoHow fast the song moves along. 2 is double speed.0.5 to 2
music_positionJumps to another part of the song.a whole number, 0 or more

Pitch and tempo are separate, so you can make a song faster without raising its notes, or lower its notes without slowing it. A value outside the range is pulled back to the nearest end of it.

music_position is not a time. A song is stored as a list of patterns played in order, and the position is a place in that list. Position 3 means "the fourth pattern", not "three seconds in". The engine cannot tell you how long a song's list is, so asking for a position past the end is your mistake to avoid.

All three settings go back to normal - pitch 1, tempo 1, position 0 - the moment a different song starts. If you want a slowed-down song to stay slowed down after a room change, set it again after the new song begins.

Sound effects

sound_play plays a sound once at a volume. You do not have to keep anything from it; the sound plays and ends by itself. The volume you pass is multiplied by the game-wide sound volume, which sound_gain sets (0 to 1). That makes sound_gain the natural home for a "sound effects volume" option. It only affects sounds started after you call it. A sound already playing keeps the volume it started with, because the hardware has no volume control for a sound in flight.

Four functions find a sound that is already playing, by naming the sound:

sound_play has no pan or speed argument. To pan or change the speed of a sound, call sound_pan or sound_speed right after sound_play, in the same event. Both calls happen before the next frame, so nothing is heard at the wrong setting.

Changing a sound's speed also changes its pitch. A recording played at double speed sounds twice as fast and an octave higher, and there is no way to separate the two. A song can do better because it is stored as notes, not a recording - that is why music has two separate controls and a sound has one.

Managing sounds in the Music tab

The Music tab lists your songs and sound effects. Click a sound to slide out a drawer with its name, its facts, Play (it turns into Stop), Remove from game, Trim, Change sample rate, Convert to .mod and Undo. Click a song to open it in the music editor. Right-click any row for Rename, Delete, Duplicate, Move to Group..., and for sounds Trim... and Sample rate.... Add Existing... takes sounds from another project: pick from the left list, they glide to the right list, then press Import selected or Import all. Create Group makes a folder, and you can drag songs and sounds into it.

What the hardware can play at once

These are the real limits.

Every song and every sound is stored in the ROM (the game file), so both count toward its size. See What goes in the ROM.

Music on the Game Boy and Game Boy Color

The Game Boy and the Game Boy Color make music a different way from the GBA. They have four channels and nothing else: two square-wave channels (a buzzy, beeping tone), one wave channel (a softer tone you can shape) and one noise channel (for drums, hisses and crashes). A song is a score for those four channels. The Game Boy cannot play a recording as music, so an mp3, a wav or any other recording cannot become Game Boy music, and the recording converter in the Music tab does not apply to these consoles.

Two kinds of song work.

The easiest way to write a Game Boy song is the music editor in the Music tab. It gives you exactly these four channels, saves a .uge song, and lists anything the Game Boy cannot play before you build. Your first song in five minutes is a short walk-through, and The music editor on each console lists the limits. The rest of this section is for writing a .mod in another tracker.

Writing a .mod for the Game Boy

The four tracks are the four channels, and the instrument numbers (not the instrument sounds, which are ignored) choose what each channel plays.

TrackchannelInstrument numbers
1Square channel 11 to 4 choose the shape of the wave: 1 is 25%, 2 is 50%, 3 is 75%, 4 is 12.5%
2Square channel 2The same as track 1
3Wave channel1 to 15 choose one of eight waves: 1 to 7 are waves 1 to 7, and 8 to 15 are waves 1 to 8 again
4Noise channel16 to 31 choose one of sixteen noises: the first eight are the harsher, shorter-pattern noises and the next eight the smoother ones

A Game Boy song plays at the Game Boy's own pace, so a song does not sound exactly as it would in a tracker that plays at the Amiga's. Speed is counted in ticks per row.

The pitch, tempo and panning of a song cannot be changed while it plays on these consoles, so music_pitch, music_tempo and sound_pan have no effect there.

When a song is refused

A .mod that breaks the rules above is not changed or half-converted. The game does not build, and the Problems list names every spot to change: the position in the song, the pattern, the row and the track, with the reason. For example, a message says that sample 7 on track 1 is outside the shape numbers 1 to 4, or that a noise note has no instrument number. Up to 24 spots are listed, and if there are more it says how many remain.

To fix it, open the song in your tracker, change each place the list names (use an allowed effect, put the instrument number on the note's row, use an instrument in the right range for the track), save it and build again. A file that is not a four-track .mod at all is refused with a message saying so; make the song as a four-track .mod in the way above, or use a hUGETracker .uge song instead.

Example

The title screen starts its song in its Create event. music_play starts it at once, with no fade, which is right because nothing was playing before.

// Create event of the title screen object
music_play(song_title, true, 1);   // the song, loop it, full volume

A sound effect is a different call. This is the Step event of the player. Each time A goes down the coin sound plays, and then it is moved a little toward the right speaker:

// Step event of the player
if (btn_pressed(BTN_A)) {
    sound_play(snd_coin, 1);
    sound_pan(snd_coin, 0.5);   // slightly to the right
}

When the player later walks into the boss room, ask for a fade instead of a cut. If the boss song is already playing this does nothing, so it is safe to call every frame:

// Step event of the boss room's door
music_fade_to(song_boss, true, 1);
See also: music_fade_tomusic_playmusic_fadingmusic_volumesound_playSound effectssound_speedmusic_tempoSaving a gameThe music editor